AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

ENT.maxZombies = 200
totalZombies = totalZombies or 0

function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("zombieSpawner") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 28)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it 
		
		return ent -- You need to return the entity to make it work
end

function ENT:Initialize()
	util.AddNetworkString("startTheBar")

	self:SetModel("models/props_junk/sawblade001a.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)

	self:SetCollisionGroup( COLLISION_GROUP_WORLD )



	local phys = self:GetPhysicsObject()
	self.nodupe = true
	self.ShareGravgun = true

	phys:SetMass(250)
	phys:Wake()

	timer.Simple(0.1, function()
		self.spawners = {}
		for i=1, 6 do
			local radius = 75
			self.spawners[i] = ents.Create("prop_physics")
			self.spawners[i]:SetModel("models/props_junk/sawblade001a.mdl")
			local j = self.Entity:GetPos().x
			local k = self.Entity:GetPos().y

			local x = radius * math.cos(i)+j
			local y = radius * math.sin(i)+k
			local z = self.Entity:GetPos().z

			self.spawners[i]:SetPos(Vector(x,y, z))
			self.spawners[i]:Spawn()
			self.spawners[i]:SetParent(self.Entity)
			self.spawners[i]:SetCollisionGroup( COLLISION_GROUP_WORLD )
		end
	end)


end

local zombieTypes = {"npc_zombie", "npc_fastzombie"}

ENT.timePass = CurTime()
function ENT:Think()
	if not tod then return end
	local liteTime = tod.GetCycle()
	if self.timePass<CurTime()-10 and liteTime and liteTime<=0.2 then
		for k,v in pairs(self.spawners) do
			
			tr = util.QuickTrace(v:GetPos() + Vector(0,0,75), Vector(0,0,-1), v)

			if (not tr.Entity:IsNPC()) then
				self:spawnZombie(v:GetPos() + Vector(0,0,50))
			end
		end
		self.timePass = CurTime()
	elseif self.timePass<CurTime()-10 then
		totalZombies=0
		for k,v in pairs(ents.GetAll()) do
			if string.find(v:GetClass(), "npc_") then
				v:Ignite(7,0)
				timer.Simple(10,function()
					v:Remove() 
				end)
			end
		end
		self.timePass = CurTime()
	end
end

function ENT:spawnZombie(pos)
	if totalZombies < self.maxZombies then
		local ent = ents.Create(zombieTypes[math.random(1,#zombieTypes)])
		ent:SetPos(pos)
		ent:Spawn()
		totalZombies = totalZombies + 1
	end
end

function zombieNoCollider(e1, e2)
	if e1:IsNPC() and e2:IsNPC() then return false end
end

hook.Add("ShouldCollide", "stopzombiecollides", zombieNoCollider)